-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzad8.py
92 lines (69 loc) · 1.74 KB
/
zad8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from copy import deepcopy
def is_consistent(G: 'graph represented using adjacency matrix'):
n = len(G)
visited = [False] * n
def dfs(i):
visited[i] = True
for j in range(n):
if G[i][j] and not visited[j]:
dfs(j)
dfs(0)
return all(visited)
def is_eulerian(G: 'graph represented using adjacency matrix'):
if not is_consistent(G): return False
n = len(G)
for i in range(n):
deg = 0
for j in range(n):
if G[i][j]: deg += 1
if deg % 2: return False
return True
def euler(G: 'graph represented using adjacency matrix'):
if not is_eulerian(G): return None
n = len(G)
result = []
def dfs(i):
for j in range(n):
# If an edge hasn't been visited yet
if G[i][j] == 1:
G[i][j] = G[j][i] = -1
dfs(j)
result.append(i)
dfs(0)
# Restore original values of edges (replace -1 with 1)
for i in range(n):
for j in range(n):
G[i][j] = abs(G[i][j])
return result
# n = 31
# G = [[1] * n for _ in range(n)]
# for i in range(n): G[i][i] = 0
# print(*G)
# print(is_eulerian(G))
# print(euler(G))
G = [[0, 1, 1, 0, 0, 0],
[1, 0, 1, 1, 0, 1],
[1, 1, 0, 0, 1, 1],
[0, 1, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1],
[0, 1, 1, 1, 1, 0]]
GG = deepcopy(G)
cycle = euler(G)
print(cycle)
if cycle is None:
print("Błąd (1)!")
exit(0)
u = cycle[0]
for v in cycle[1:]:
if not GG[u][v]:
print("Błąd (2)!")
exit(0)
GG[u][v] = False
GG[v][u] = False
u = v
for i in range(len(GG)):
for j in range(len(GG)):
if GG[i][j]:
print("Błąd (3)!")
exit(0)
print("OK")